import {
    formatSeconds
  } from '../../../utils/util.js'
  
  let app = getApp()
  Page({
    data: {
      spinShow: false,
      paperId: null,
      examId: null,
  
      form: {},
      timer: null,
      doTime: 0,
      remainTime: 0,
      remainTimeStr: '',
      modalShow: false,
      result: 0,
      timeOutShow: false,
      queryParam: {
        examId: "",
      },
 
    },
    onLoad: function(options) {
      let paperId = options.id;
      let _this = this;
  
      _this.setData({
          examId:options.examId,
          ['queryParam.examId']: options.examId,

      })
      _this.setData({
        spinShow: true
      });
      app.formPost('/api/wx/student/exampaper/select/' + paperId, _this.data.queryParam)
        .then(res => {
          _this.setData({
            spinShow: false
          });
          if (res.code === 1) {
            _this.setData({
              form: res.response,
              paperId: paperId,
              remainTime: res.response.suggestTime * 60
            });
            _this.timeReduce()
          }else if(res.code == 401){
  
              wx.qy.login({
                  success(wxres) {
                      console.log("code是"+wxres.code)
      
                    if (wxres.code) {
                      app.formGet('/inApi/qywx/student/auth/appletLogin', {
                        "code": wxres.code
                      }).then(res => {
                        if (res.code == 1) {
                          wx.setStorageSync('token', res.response.token)
                          wx.reLaunch({
                            url: '/pages/index/index',
                          });
                        }
                        else {
                          _this.message(res.message, '您无该系统的权限，请联系系统管理员')
          
                              wx.reLaunch({
                            url: '/pages/403/index',
                          });
                        }
                      }).catch(e => {
                        _this.message(e, 'error')
                      })  
                    } else {
                      _this.message(res.errMsg, 'error')
                    }
                  }
                })
      
            }else if(res.code == 305){
                wx.showToast({
                    title: "无权限继续考试，即将为您跳转首页！",
                  });
                  
         
                  setTimeout(()=>
   {
    wx.switchTab({
        url: '/pages/index/index',
      })
   }, 1000)
            }
          
        }).catch(e => {
          _this.setData({
            spinShow: false
          });
          app.message(e, 'error')
        })
    },
    timeReduce() {
      let _this = this
      let timer = setInterval(function() {
        let remainTime = _this.data.remainTime
        if (remainTime <= 0) {
          _this.timeOut()
        } else {
          _this.setData({
            remainTime: remainTime - 1,
            remainTimeStr: formatSeconds(remainTime),
            doTime: _this.data.doTime + 1
          });
        }
      }, 1000)
      _this.setData({
        timer: timer
      });
    },
    onUnload() {
      clearInterval(this.data.timer)
    },
    returnRecord() {
      wx.reLaunch({
        url: '/pages/record/index',
      });
    },
    timeOut() {
      clearInterval(this.data.timer)
      this.setData({
        timeOutShow: true
      });
    },
    formSubmit: function(e) {
      let _this = this
      if (this.data.timer) {
        clearInterval(this.data.timer)
      }
      wx.showLoading({
        title: '提交中',
        mask: true
      })
      e.detail.value.id = this.data.paperId,
      e.detail.value.doTime = this.data.doTime,
      e.detail.value.examId = this.data.examId,
  
      app.formPost('/api/wx/student/exampaper/answer/answerSubmit', e.detail.value)
        .then(res => {
          if (res.code === 1) {
            _this.setData({
              modalShow: true,
              result: res.response
            });
          }else if(res.code == 401){
  
              wx.qy.login({
                  success(wxres) {
                      console.log("code是"+wxres.code)
      
                    if (wxres.code) {
                      app.formGet('/inApi/qywx/student/auth/appletLogin', {
                        "code": wxres.code
                      }).then(res => {
                        if (res.code == 1) {
                          wx.setStorageSync('token', res.response.token)
                          wx.reLaunch({
                            url: '/pages/index/index',
                          });
                        }
                        else {
                          _this.message(res.message, '您无该系统的权限，请联系系统管理员')
          
                              wx.reLaunch({
                            url: '/pages/403/index',
                          });
                        }
                      }).catch(e => {
                        _this.message(e, 'error')
                      })  
                    } else {
                      _this.message(res.errMsg, 'error')
                    }
                  }
                })
      
            } else {
            app.message(res.response, 'error')
          }
          wx.hideLoading()
        }).catch(e => {
          wx.hideLoading()
          app.message(e, 'error')
        })
    }
  })